Ex010, Lookup Table
In this example we will illustrate how to make a lookup table using DBLookupComboBox and
two Table objects.
Exercise 010: Lookup Table
In this exercies we want to make two tables: People table which contain PIN (Personal
Identification Number), Name, and Country_ID; Other table is Country table which
contain Country_ID and Country_Name. Country table will be the lookup table. Data
will be entered in main table (People) but instead of entering country number in
each People record we can select a country name from a combo box.
1. Create People table using this definition:
Note:
If you are not familiar with designing tables you can refer to Ex007 (Telephone
directory) and look to steps (1..7)
----------------------------------
Field Name | Type | Size | Key |
---------------------------------|
PID | + | | * |
Name | A | 30 | |
Country_ID | I | | |
----------------------------------
2. Create Country table using this definition:
----------------------------------
Field Name | Type | Size | Key |
---------------------------------|
Country_ID | + | | * |
Country_Name| A | 20 | |
----------------------------------
3. At Database Desktop enter many countries on Country table after clicking Edit
Data button.
4. At main form drop two Tables and two DataSources.
5. For both tables select DBDEMOS alias in DatabaseName property.
6. In Table1, Select 'People.db' from TableName property.
7. In Table2, Select 'Country.db' from TableName property.
8. Select Table1 on DataSource1's DataSet property.
9. Select Table2 on DataSource2's DataSet property.
10. Drop two DBEdit, DBNavigator, and DBLookupComboBox from Data Controls page.
11. On DataSource property for all these data controls select 'DataSource1'.
12. Drop three Labels from Standard page, put it at left side of DBEdit boxes and
DBLookupComboBox. Caption these labels: PID, Name, and Country.
13. For DBEdit1 DataField property select PID.
14. For DBEdit2 DataField property select Name.
15. For DBLookupComboBox1 DataField property select Country_ID.
16. In DBLookupComboBox1 set below table:
--------------------------
Property Value
--------------------------
KeyField Country_ID
ListField Country_Name
ListSource DataSource2
--------------------------
17. At Form's OnCreate event write:
Table1.Open;
Table2.Open;
18. Run the program then click (+) button to add new records.
Notes:
- You can not write or modify on ID field, because it is an auto-increment field.
Auto-increment fields automatically assign it's value.